/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-27
*/
package com.rzico.basics.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.WxPage;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.WxPageService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 页面管理控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-05-27
 */
@Api(description = "页面管理接口")
@RestController
@RequestMapping("/wxPage")
public class WxPageController extends BaseController {

    @Autowired
    private WxPageService wxPageService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 分页查询页面管理
     *
     * @return
    */
    @ApiOperation("分页查询页面管理")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mchId", value = "商户Id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "tagId", value = "标签", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "tagIds", value = "标签", dataType = "String", paramType = "query")
    })
    public CommResult<WxPage> list(String mchId,Long tagId,Long [] tagIds, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        Enterprise enterprise = enterpriseService.findByMch(mchId);
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        if (tagId!=null) {
            params.put("tagId",tagId);
        }

        if (tagIds!=null && tagIds.length>0) {
            params.put("tagIds",tagIds);
        }

        wxPageService.getHome(enterprise);

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<WxPage> list = wxPageService.selectList(params);
        PageResult<WxPage> pageResult = new PageResult<WxPage>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }


    /**
     * 查询单条页面管理
     *
     * @return
     */
    @ApiOperation("查询单条页面管理")
    @GetMapping("/find/{id}")
    public CommResult<WxPage> find(@PathVariable String id) {
        WxPage result = wxPageService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("wxPage",result);
        return CommResult.success(data);

    }


    /**
     * 获取首页接口
     *
     * @return
     */
    @ApiOperation("获取首页接口")
    @GetMapping("/getHome")
    @ApiImplicitParams({@ApiImplicitParam(name = "mchId", value = "商户号", dataType = "String", paramType = "query")
    })
    public CommResult<WxPage> getHome(String mchId) {

        Enterprise enterprise = enterpriseService.findByMch(mchId);

        WxPage result = wxPageService.getHome(enterprise);

        Map<String,Object> data = new HashMap<>();
        data.put("wxPage",result);
        return CommResult.success(data);

    }


}
